package com.supermap.dataservice.cloud.live;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.live.ArtemisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:zhoujian
 * @Desciption:
 * @date:2019-09-25 15:17
 * @version:v1.0.0
 */
@Service
public class ArtemisServiceImpl implements ArtemisService{

    private Logger logger = LoggerFactory.getLogger(ArtemisServiceImpl.class);

    /**
     * 请根据自己的appKey和appSecret更换static静态块中的三个参数. [1 host]
     * 如果你选择的是和现场环境对接,host要修改为现场环境的ip,https端口默认为443，http端口默认为80.例如10.33.25.22:443 或者10.33.25.22:80
     * appKey和appSecret请按照或得到的appKey和appSecret更改.
     * TODO 调用前先要清楚接口传入的是什么，是传入json就用doPostStringArtemis方法，下载图片doPostStringImgArtemis方法
     */
    static {
        ArtemisConfig.host = "94.191.118.83";// 代理API网关nginx服务器ip端口
        ArtemisConfig.appKey = "24932119";// 秘钥appkey
        ArtemisConfig.appSecret = "c7pmSZDtDH3YoAZ6J1O9";// 秘钥appSecret
    }
    /**
     * 能力开放平台的网站路径
     * TODO 路径不用修改，就是/artemis
     */
    private static final String ARTEMIS_PATH = "/artemis";


    /**
     * 获取区域列表
     * @return
     */
    public JSONUtils getRegionsList()
    {
        final String getCamsApi = ARTEMIS_PATH +"/api/resource/v1/regions";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getCamsApi);//根据现场环境部署确认是http还是https
            }
        };
        //设置参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 10);
        String body = jsonBody.toJSONString();
        //开始调用
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        JSONObject resultObject = JSONObject.parseObject(result);
        if (resultObject != null)
        {
            if (resultObject.getIntValue("code") == 0)
            {
                int total =  resultObject.getJSONObject("data").getIntValue("total");
                Object[] objects = resultObject.getJSONObject("data").getJSONArray("list").toArray();
                return  new JSONUtils().setData(objects).setCountSum(objects != null ? objects.length : 0);

            }else{
                logger.error(String.format("请求错误，返回错误码为:%d\n请求url:%s\n参数:%s" , resultObject.getIntValue("code") , path , body));
                return  new JSONUtils().setCountSum(0).setMsg("请求错误，返回错误码为"+ resultObject.getIntValue("code"));
            }
        }else
        {
            logger.error(String.format("请求结果为空。\n请求url:%s\n参数:%s"  , path , body));
            return new JSONUtils().setCountSum(0).setMsg("请求结果为空");
        }
    }

    /**
     * 根据区域编号获取下级监控点列表
     */
    public JSONUtils getCamerasByRegionIndexCode(String regionIndexCode)
    {
        final String getCamsApi = ARTEMIS_PATH +"/api/resource/v1/regions/regionIndexCode/cameras";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getCamsApi);//根据现场环境部署确认是http还是https
            }
        };
        //设置参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("regionIndexCode",regionIndexCode);
        jsonBody.put("pageNo", 1);
        jsonBody.put("pageSize", 10);
        String body = jsonBody.toJSONString();
        //发起请求
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        JSONObject resultObject = JSONObject.parseObject(result);
        if (resultObject != null)
        {
            if (resultObject.getIntValue("code") == 0)
            {
                int total =  resultObject.getJSONObject("data").getIntValue("total");
                Object[] objects = resultObject.getJSONObject("data").getJSONArray("list").toArray();
                if (objects != null){
                    List<JSONObject> camerasJsons = new ArrayList<>();
                    for (Object o : objects)
                    {
                        JSONObject json = (JSONObject) o;
                        System.out.println("名称: " + json.getString("cameraName")  + ", 类型: " + json.getString("cameraType") + " , 编码: " + json.getString("cameraIndexCode"));
                        String cameraName = json.getString("cameraName");
                        String cameraType = json.getString("cameraType");
                        String caneraIndexCode = json.getString("cameraIndexCode");
                        JSONObject cameraJson = new JSONObject();
                        cameraJson.put("cameraName" , cameraName);
                        cameraJson.put("cameraType" , cameraType);
                        cameraJson.put("caneraIndexCode" ,caneraIndexCode );
                        camerasJsons.add(cameraJson);
                    }
                    return  new JSONUtils().setData(camerasJsons).setCountSum(camerasJsons.size());
                }else{
                    logger.error(String.format("请求错误，返回错误码为:%d\n请求url:%s\n参数:%s" , resultObject.getIntValue("code") , path , body));
                    return  new JSONUtils().setCountSum(0).setMsg("请求错误，返回错误码为"+ resultObject.getIntValue("code"));
                }
            }else{
                logger.error(String.format("请求错误，返回错误码为:%d\n请求url:%s\n参数:%s" , resultObject.getIntValue("code") , path , body));
                return  new JSONUtils().setCountSum(0).setMsg("请求错误，返回错误码为"+ resultObject.getIntValue("code"));
            }
        }else
        {
            logger.error(String.format("请求结果为空。\n请求url:%s\n参数:%s"  , path , body));
            return new JSONUtils().setCountSum(0).setMsg("请求结果为空");
        }
    }
    /**
     * 获取监控点预览取流URL
     * @return
     */
    public JSONUtils getCameraPreviewURLs(String cameraIndexCode)
    {
        final String getCamsApi = ARTEMIS_PATH +"/api/video/v1/cameras/previewURLs";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getCamsApi);//根据现场环境部署确认是http还是https
            }
        };
        //设置参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        String body = jsonBody.toJSONString();
        //发起请求
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        JSONObject resultObject = JSONObject.parseObject(result);
        if (resultObject != null){
            int code = resultObject.getIntValue("code");
            if (code == 0){
                //请求成功,取出data中的数据
                JSONObject jsonObject = resultObject.getJSONObject("data");
                return new JSONUtils().setData(jsonObject);
            }else {
                //请求异常
                logger.error(String.format("请求错误，返回错误码为:%d\n请求url:%s\n参数:%s" , code , path , body));
                return  new JSONUtils().setCountSum(0).setMsg("请求错误，返回错误码为"+ code);
            }
        }else{
            //请求结果返回为空
            logger.error(String.format("请求结果为空。\n请求url:%s\n参数:%s"  , path , body));
            return new JSONUtils().setCountSum(0).setMsg("请求结果为空");
        }
    }
    /**
     * 手动抓图
     * @return
     */
    public JSONUtils getManualCapture(String cameraIndexCode)
    {
        final String getCamsApi = ARTEMIS_PATH +"/api/video/v1/manualCapture";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("http://", getCamsApi);//根据现场环境部署确认是http还是https
            }
        };
        //设置参数
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        String body = jsonBody.toJSONString();
        //发起请求
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        JSONObject resultObject = JSONObject.parseObject(result);
        if (resultObject != null)
        {
            String code = resultObject.getString("code");
            if ("0".equals(code))
            {
                //请求成功,取出data里面的数据
                JSONObject jsonObject = resultObject.getJSONObject("data");
                return new JSONUtils().setData(jsonObject);
            }else{
                //请求失败
                logger.error(String.format("请求错误，返回错误码为:%s\n请求url:%s\n参数:%s" , code , path , body));
                return  new JSONUtils().setCountSum(0).setMsg("请求错误，返回错误码为"+ code);
            }
        }else{
            logger.error(String.format("请求结果为空。\n请求url:%s\n参数:%s"  , path , body));
            return new JSONUtils().setCountSum(0).setMsg("请求结果为空");
        }
    }
}